﻿using ConsoleApp2.Service;
using ConsoleApp2.User;

namespace ConsoleApp2.Impl;

public class CodeServiceImpl : CodeService
{
    const string CODE_SOURCE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789";
    const int CODE_LENGTH = 6;

    public Code CreateCode()
    {
        Code code = new Code();
        Random r = new Random();
        for (int i = 0; i < CODE_LENGTH; i++)
        {
            int num = r.Next(CODE_SOURCE.Length);
            code.OldCode += CODE_SOURCE[num] + " ";
            code.NewCode += CODE_SOURCE[num];
        }
        code.Expiration = DateTime.Now.AddSeconds(50);
        return code;
    }

    public bool CheckCode(Code systemCode)
    {
        if (systemCode.UserInputCode.ToUpper() == systemCode.NewCode.ToUpper())
        {
            return true;
        }
        return false;
    }

    public bool CheckCodeExpiration(Code code)
    {
        if (code.Expiration > DateTime.Now)
        {
            return true;
        }
        return false;
    }
}